########
# Usage: Rscript detail_longRNA_boxplot1.R 'FGA' "TRUE" "Healthy,Benign,BRCA,HCC"
#        Rscript detail_longRNA_boxplot1.R 'FGA' "FALSE" "Healthy,Benign,BRCA,HCC"

# 传入参数： target = 'FGA'  
#            log = "TRUE"                   # log = "FALSE"   or   log = "TRUE"
#            cohort.selected = c('Healthy', 'Benign', 'BRCA', 'HCC')

# 默认传入除（ESCC, GBM, MEL）外的所有cohort
#        Rscript detail_longRNA_boxplot1.R 'FGA' "TRUE" "Urine,Bile,CSF,Healthy,Benign,BRCA,CHD,CRC,GC,HCC,KIRC,ML,OV,PAAD,SCLC"
#        Rscript detail_longRNA_boxplot1.R 'FGA' "FALSE" "Urine,Bile,CSF,Healthy,Benign,BRCA,CHD,CRC,GC,HCC,KIRC,ML,OV,PAAD,SCLC"
#
########
library(ggplot2)
library(ggpubr)
library(RColorBrewer)

library(optparse)

option_list <- list(
  make_option("--r", default = "", type = "character", help = "rna file"),
  make_option("--t", default = "", type = "character", help = "target"),
  make_option("--i", default = "", type = "character", help = "target file"),
  make_option("--l", default = "", type = "character", help = "log"),
  make_option("--c", default = "", type = "character", help = "cohort selected"),
  make_option("--o", default = "", type = "character", help = "output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

Args = commandArgs()
target = opt$t
log = opt$l
cohort.selected = opt$c
cohort.selected = as.vector(unlist(strsplit(cohort.selected, ',')))

# target = 'FGA'
# log = "FALSE"                   # log = "FALSE"   or   log = "TRUE"
# cohort.selected = c('Healthy', 'Benign', 'BRCA', 'HCC', 'SCLC')
# cohort.selected = c("Urine","Bile","CSF","Healthy","Benign","BRCA","CHD","CRC","GC","HCC","KIRC","ML","OV","PAAD","SCLC")
# path = "D:\\exo\\905\\submit 7\\detail_longRNA"
# setwd(path)

targetType = 'longRNA'
w = 12
h = 6
outfile = opt$o

infile = opt$i
data.target = read.csv(infile, sep = ',', header = 1, row.names = NULL)
colnames(data.target) = c('sample', 'type', 'cohort', 'sampleSize', 'TPM')                  ### sampleSize
data.target = data.target[data.target$cohort %in% cohort.selected,]
data.target$label = paste(data.target$cohort, '\n', '(', data.target$sampleSize, ')', sep = '')   ### label

cohortsNumber = length(cohort.selected)
if (cohortsNumber == 15) {               ### 默认选择除（ESCC, GBM, MEL）外的所有cohort， 且固定cohort顺序
  labelOrder = c("Urine\n(16)", "CSF\n(5)", "Bile\n(15)", "Healthy\n(118)", "Benign\n(130)", "BRCA\n(140)", "CHD\n(12)", "CRC\n(35)", "GC\n(9)", "HCC\n(112)", "KIRC\n(15)", "ML\n(28)", "OV\n(30)", "PAAD\n(164)", "SCLC\n(36)")
  data.target$label = factor(data.target$label, levels = labelOrder)
}

cols = c(brewer.pal(9, "Pastel1"), brewer.pal(9, "Set3"))
if (log == "FALSE") {
  pdf(outfile, width = w, height = h)
  p = ggplot(data.target, aes(x = label, y = TPM, fill = label)) +                   #### label
    geom_boxplot(linetype = "dashed", width = 0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color = "#525252", size = 0.5) +
    stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..), width = 0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color = "#525252", size = 0.5) +
    stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..), color = "#525252", size = 0.5, width = 0.3) +
    stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..), color = "#525252", size = 0.5, width = 0.3) +
    scale_fill_manual(values = cols) +
    xlab('') +
    ylab('TPM') +
    ggtitle(target) +
    theme_bw() +
    theme(panel.grid.major.x = element_blank(),
          panel.grid = element_line(colour = 'gray')) +
    theme(axis.title = element_text(size = 12, face = "bold")) +
    theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5)) +
    guides(fill = FALSE, colour = FALSE) +
    theme(panel.border = element_rect(linetype = 'solid', size = 1.2, fill = NA),
          axis.text.y = element_text(face = 'bold', color = 'black', size = 10, angle = 360),
          axis.text.x = element_text(face = 'bold', color = 'black', size = 10, angle = 360, hjust = 0.5),         #### angle = 360, hjust = 0.5
          axis.title = element_text(size = 14, face = "bold"))
}else {
  data.target$TPM = log2(data.target$TPM + 1)
  pdf(outfile, width = w, height = h)
  p = ggplot(data.target, aes(x = label, y = TPM, fill = label)) +                      #### label
    geom_boxplot(linetype = "dashed", width = 0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color = "#525252", size = 0.5) +
    stat_boxplot(aes(ymin = ..lower.., ymax = ..upper..), width = 0.6, outlier.shape = 1, outlier.size = 0.8, outlier.colour = 'black', color = "#525252", size = 0.5) +
    stat_boxplot(geom = "errorbar", aes(ymin = ..ymax..), color = "#525252", size = 0.5, width = 0.3) +
    stat_boxplot(geom = "errorbar", aes(ymax = ..ymin..), color = "#525252", size = 0.5, width = 0.3) +
    scale_fill_manual(values = cols) +
    xlab('') +
    ylab('log2(TPM+1)') +
    ggtitle(target) +
    theme_bw() +
    theme(panel.grid.major.x = element_blank(),
          panel.grid = element_line(colour = 'gray')) +
    theme(axis.title = element_text(size = 12, face = "bold")) +
    theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5)) +
    guides(fill = FALSE, colour = FALSE) +
    theme(panel.border = element_rect(linetype = 'solid', size = 1.2, fill = NA),
          axis.text.y = element_text(face = 'bold', color = 'black', size = 10, angle = 360),
          axis.text.x = element_text(face = 'bold', color = 'black', size = 10, angle = 360, hjust = 0.5),          #### angle = 360, hjust = 0.5
          axis.title = element_text(size = 14, face = "bold"))
}
print(p)
dev.off()  





